<?php
/**
 * Created by PhpStorm.
 * User: Jerry
 * Date: 2017/3/15
 * Time: 14:00
 */

namespace SDXApi\Service;


use SDXApi\Abstracts\TemplateAbstract;
use SDXApi\Model\AlbumTemplateTextModel;

/**
 * Class AlbumTemplateTextService
 * @package SDXApi\Service
 */
class AlbumTemplateTextService extends TemplateAbstract
{
	/**
	 * @var AlbumTemplateTextModel
	 */
	protected $albumTemplateTextModel;

	/**
	 * AlbumTemplateTextService constructor.
	 */
	public function __construct()
	{
		$this->albumTemplateTextModel = new AlbumTemplateTextModel();
	}

	/**
	 * 获取模版字体数据
	 * Created by Jerry
	 * @param array $template
	 * @param int $userId
	 * @param int $wid
	 * @return bool|mixed
	 */
	public function handle($template = array(), $userId = 0, $wid = 8)
	{
		$cacheName = 'album_template_text_id_' . $template['id'];

		$data = $this->getCache($cacheName);

		if (empty($data)) {
			$data = $this->checkText($template);
			$this->cacheData($cacheName, $data, mt_rand(3600, 4200));
		}

		return $data;
	}


	/**
	 * 处理字体
	 * Created by Jerry
	 * @param array $template
	 * @return bool|mixed|arrayre
	 */
	protected function checkText($template = array())
	{
		if (empty($template)) {
			return false;
		}

		$data = $this->findAlbumTemplateTextByTemplateId($template['id']);

		$template['content'] = $data['content'];
		return $template;
	}

	/**
	 * Created by Jerry
	 * @param int $id
	 * @return bool|mixed
	 */
	protected function findAlbumTemplateTextByTemplateId($id = 0)
	{
		if (empty($id)) {
			return false;
		}

		return $this->albumTemplateTextModel->findAlbumTextByTemplateId($id);
	}
}